//
//  TopView.m
//  QuickCure
//
//  Created by 源 何 on 16/9/13.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "TopView.h"
#import "TopViewModel.h"
#import <UIImageView+WebCache.h>
#import "ProvTableViewController.h"
@interface TopView ()

//顶部view的数组
@property (nonatomic, strong) NSArray *topViewList;

@property (nonatomic, copy) NSString *location;

@property (nonatomic, strong) UIImageView *topViewImageView;

@property (nonatomic, strong) UILabel *weatherLabel;

@property (nonatomic, strong) UILabel *cityLabel;

@property (nonatomic, strong) UILabel *tempLabel;

@property (nonatomic, strong) UILabel *dateLabel;

@end

@implementation TopView

- (void)setLocation:(NSString *)location{
    
    _location = location;
    
    [TopViewModel topViewListWithSuccess:^(NSArray<TopViewModel *> *array) {
        
        self.topViewList = array;
        
//        NSLog(@"%@",self.topViewList);
    } error:^{
    } location:self.location];
}

- (void)setTopViewList:(NSArray *)topViewList{
    _topViewList = topViewList;
    
    TopViewModel *model = topViewList[0];
    
    //图片
    NSURL *url = [NSURL URLWithString:model.dayPictureUrl];

    [_topViewImageView sd_setImageWithURL:url];
    //天气
    [_weatherLabel setText:model.weather];
    
    //城市
    [_cityLabel setText:self.location];
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.location attributes:attribtDic];
    _cityLabel.attributedText = attribtStr;
    
    //温度
    [_tempLabel setText:[NSString stringWithFormat:@"温度: %@",model.temperature]];
    //日期
    NSString *str = [NSString stringWithFormat:@"日期:%@",model.date] ;

    NSString *tempStr = [str substringToIndex:13];
    [_dateLabel setText:tempStr];

}


#pragma mark
#pragma mark -- 初始化构造方法
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark
#pragma mark -- 加载UI界面
- (void)setupUI{
    
    _location = @"上海";
    
    [TopViewModel topViewListWithSuccess:^(NSArray<TopViewModel *> *topList) {
        self.topViewList = topList;
        
        TopViewModel *model = self.topViewList[0];
        
        self.backgroundColor = [UIColor colorWithRed:0.6667 green:0.9412 blue:0.8627 alpha:1.0];
        
        //图片
        NSURL *url = [NSURL URLWithString:model.dayPictureUrl];
        _topViewImageView = [[UIImageView alloc] init];
        [_topViewImageView sd_setImageWithURL:url];
        _topViewImageView.center = CGPointMake(self.frame.size.width/10, self.frame.size.height/2);
        _topViewImageView.bounds = CGRectMake(0, 0, 40, 40);
        [self addSubview:_topViewImageView];
        
        //天气
        _weatherLabel = [[UILabel alloc] init];
        [_weatherLabel setText:model.weather];
        [_weatherLabel setFont:[UIFont systemFontOfSize:14]];
        
        _weatherLabel.numberOfLines = 2;
        [self addSubview:_weatherLabel];
        [_weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self).multipliedBy(0.5);
            make.centerY.equalTo(self);
            make.width.height.equalTo(@55);
        }];
        [_weatherLabel setTextColor:[UIColor colorWithRed:0.0941 green:0.7373 blue:0.7333 alpha:1.0]];
        
        //城市
        _cityLabel = [[UILabel alloc] init];
        [_cityLabel setText:self.location];
        [_cityLabel setFont:[UIFont systemFontOfSize:17]];
        [_cityLabel sizeToFit];
        _cityLabel.center = CGPointMake(self.frame.size.width*1.5/4, self.frame.size.height/2);
        [_cityLabel setTextColor:[UIColor colorWithRed:0.0941 green:0.7373 blue:0.7333 alpha:1.0]];
        
        //设置下划线
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:_cityLabel.text attributes:attribtDic];
        _cityLabel.attributedText = attribtStr;
        _cityLabel.userInteractionEnabled = YES;
    
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [_cityLabel addGestureRecognizer:tap];
        [self addSubview:_cityLabel];
        
        //温度
        _tempLabel = [[UILabel alloc] init];
        [_tempLabel setText:[NSString stringWithFormat:@"温度: %@",model.temperature]];
        [_tempLabel setFont:[UIFont systemFontOfSize:15]];
        _tempLabel.center = CGPointMake(self.frame.size.width*0.6,self.frame.size.height*0.2);
        [_tempLabel setTextColor:[UIColor colorWithRed:0.0941 green:0.7373 blue:0.7333 alpha:1.0]];
        [_tempLabel setTextAlignment:NSTextAlignmentCenter];
        [_tempLabel sizeToFit];
        [self addSubview:_tempLabel];
        
        //日期
        _dateLabel = [[UILabel alloc] init];
        NSString *str = [NSString stringWithFormat:@"日期:%@",model.date] ;
        //截取字符串
        NSString *tempStr = [str substringToIndex:13];
        
        [_dateLabel setText:tempStr];
        [_dateLabel setFont:[UIFont systemFontOfSize:14]];
        _dateLabel.center = CGPointMake(self.frame.size.width*0.57,self.frame.size.height*0.6);
        [_dateLabel setTextColor:[UIColor colorWithRed:0.0941 green:0.7373 blue:0.7333 alpha:1.0]];
        [_dateLabel setTextAlignment:NSTextAlignmentCenter];
        [_dateLabel sizeToFit];
        [self addSubview:_dateLabel];
        
    } error:^{
    } location:self.location];
}

- (void)tap:(UIGestureRecognizer *)tap{
    
    ProvTableViewController *pv = [[ProvTableViewController alloc] init];
    
    pv.provBlock = ^(NSString *str){
        
        self.location = str;
        
    };

    //找到他所在的父视图控制器，通过父视图来执行push操作
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] && object != nil) {
        object = [object nextResponder];
    }
    UIViewController *superController = (UIViewController*)object;
    
    [superController.navigationController pushViewController:pv animated:YES];
}
@end
